/* eslint-disable @typescript-eslint/no-explicit-any */
import { defineMock } from 'vite-plugin-mock-dev-server'
import { incrementIdFromQueryParam, buildPageData, buildOkData } from './util'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import mockjs from 'mockjs'

export default defineMock([
  {
    url: '/local-mock-api/Person/post',
    method: 'POST',
    status: 200,
    delay: 1500,
    response(req, res) {
      const { query, body, params, headers } = req
      console.log(query, body, params, headers)

      const { size } = body
      // 设置总页数
      const totalPage = 3
      const lastPageSize = parseInt(`${size / 2}`)
      const total = size * 2 + lastPageSize
      const isLastPage = body.page >= totalPage
      const page = body.page >= totalPage ? totalPage : body.page
      const incrementCall = incrementIdFromQueryParam({ page, size })
      const map = new Map()
      map.set(`list|${isLastPage ? lastPageSize : size}`, [
        {
          // title: '@ctitle(5, 25)',
          // content: '@cparagraph(2,4)', // 最少2段落，最多4段落
          // id:"@id",
          // author: '@cname()',
          // date: '@date("yyyy-MM-dd")',
          // email: '@email()',
          // ip: 'ip()',
          // region: 'region()', // 华中，华南，华西这种全国性区域
          // province: 'province()', // 省份
          // city: '@city()', // 城市: 重庆市
          // city: '@city(true)', // 城市: 安徽省 六安市
          // county: '@county()', // 五原县
          // county: '@county(true)', // 新疆维吾尔自治区 巴音郭楞蒙古自治州 和静县
          // 'pic|1-36': 1, // 产生1-36的随机数字
          // image: Random.image('200x100'), // 生成一个宽200，高100的图片的url地址
          // image: Random.image('200x100', '#FF6600'), // 生成一个宽200，高100，背景色为 #FF6600，的图片的url地址
          // image: Random.image('200x100', '#4A7BF7', 'Hello'), // 生成一个宽200，高100，背景色为 #4A7BF7，图片文字为: Hello 的图片的url地址
          // image: Random.image('200x100', '#894FC4', '#FFF', 'png', 'Hello'), // 生成一个宽200，高100，背景色为 #894FC4，图片文字为: Hello 且文字颜色为 #FFF 的png图片的url地址

          name: '<div><font color="red">红色字体</font><script>window.location.reload()</script><style>html,body{background:green}</style></div>',
          'age|18-70': 1,
        },
      ])
      const tmpObj = [...map.entries()].reduce(
        (obj, [key, value]) => ((obj[key] = value), obj),
        {}
      )
      const obj = mockjs.mock(tmpObj) as any
      if (obj.list instanceof Array) {
        obj.list = (obj.list as any[]).map(item => ({
          ...item,
          male: mockjs.Random.pick(['male', 'female']),
          idx: incrementCall(),
        }))
      } else {
        obj.list = [{ ...obj.list, idx: incrementCall() }]
      }

      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(buildPageData(obj.list, total, { page, size })))
    },
  },
  {
    url: '/local-mock-api/Person/add',
    method: 'POST',
    status: 200,
    delay: 1500,
    response(req, res) {
      const { query, body, params, headers } = req
      console.log(query, body, params, headers)

      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(buildOkData()))
    },
  },
  {
    url: '/local-mock-api/Person/edit',
    method: 'PUT',
    status: 200,
    delay: 1500,
    response(req, res) {
      const { query, body, params, headers } = req
      console.log(query, body, params, headers)

      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(buildOkData(1)))
    },
  },
  {
    url: '/local-mock-api/Person/del/:id',
    method: 'DELETE',
    status: 200,
    delay: 1500,
    response(req, res) {
      const { query, body, params, headers } = req
      console.log(query, body, params, headers)

      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(buildOkData()))
    },
  },
])
